/**
*      author: Y_Yao
*      created: 2024-12-13 18:07:52
**/

#include<bits/stdc++.h>

#define int LL
using namespace std;
using LL = long long;
const LL LLINF = 1e18;
const int INF = 0x3f3f3f3f;
const int N = 10 + 7;
int n;
int s[N],b[N];
bool vis[N];
int ans = INF;

void dfs(int cur,int su,int ku)
{
    if(cur != 0)
    {
        ans = min(ans,abs(su - ku));
    }

    for(int i = cur;i <= n;i++)
    {
        if(vis[i]) continue;
        vis[i] = true;
        dfs(i,su * s[i],ku + b[i]);
        vis[i] = false;
    }
}

void solve()
{
    cin >> n;
    for(int i = 1;i <= n;i++)
    {
        cin >> s[i] >> b[i];
    }
    dfs(0,1,0);
    cout << ans << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    //cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}